﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;

namespace EFong.View.Documentation
{
    public partial class Frm_NewJourneyOrder : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_NewJourneyOrder()
        {
            InitializeComponent();
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
            {
                this.save();
            };
        }

        private void save()
        {
            List<Contract.JourneyOrder> newJourneyOrders = null;

            UIOperManager.SaveData(
                () =>
                {
                    if (string.IsNullOrEmpty(txt_Start.GetText()))
                    {
                        MsgBox.Info("请输入开始单号.");
                        return false;
                    }

                    if (string.IsNullOrEmpty(txt_End.GetText()))
                    {
                        MsgBox.Info("请输入结束单号.");
                        return false;
                    }

                    return true;
                },
                () =>
                {
                    newJourneyOrders = getJourneyOrders();

                   return _masterProxy.NewJourneyOrder(newJourneyOrders).IsSuccess;
                },
                MsgResource.Save_Complete,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                });
        }

        private List<Contract.JourneyOrder> getJourneyOrders()
        {
            List<Contract.JourneyOrder> newJourneyOrders = new List<Contract.JourneyOrder>();

            List<string> refs = DocumentHelper.GetRefList(txt_Start.GetText(), txt_End.GetText());

            if (refs != null && refs.Count > 0)
            {
                foreach (var eachRef in refs)
                {
                    newJourneyOrders.Add(new Contract.JourneyOrder
                                            {
                                                Type = txt_Type.GetText(),
                                                Ref = eachRef
                                            });
                }
            }

            return newJourneyOrders;
        }
    }
}
